﻿using GeneralDashboard.ViewModel;
using ModernWpf;
using ModernWpf.SampleApp.Presets;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ModernWpf.Controls;
using Page = System.Windows.Controls.Page;

namespace GeneralDashboard.Navigation
{
    /// <summary>
    /// NavigationRootPage.xaml 的交互逻辑
    /// </summary>
    public partial class NavigationRootPage : UserControl
    {
        private readonly Page _designerPage;
        private readonly Page _dataSourcePage;
        private readonly UserControl _ProtocolDesignerDock;
        private readonly UserControl _UIDesignerDock;
        private readonly UserControl _ProtocolExpander;
        private readonly UserControl _UIExpander;



        public Page CurrentPage
        {
            get { return (Page)GetValue(CurrentPageProperty); }
            set { SetValue(CurrentPageProperty, value); }
        }

        public UserControl CurrentBorder
        {
            get { return (UserControl)GetValue(CurrentBorderProperty); }
            set { SetValue(CurrentBorderProperty, value); }
        }
        public UserControl CurrentExpender
        {
            get { return (UserControl)GetValue(CurrentExpenderProperty); }
            set { SetValue(CurrentExpenderProperty, value); }
        }

        public static readonly DependencyProperty CurrentExpenderProperty =
            DependencyProperty.Register("CurrentExpender", typeof(UserControl), typeof(NavigationRootPage), new PropertyMetadata(null));
        public static readonly DependencyProperty CurrentBorderProperty =
            DependencyProperty.Register("CurrentBorder", typeof(UserControl), typeof(NavigationRootPage), new PropertyMetadata(null));
        // using a dependencyproperty as the backing store for currentpage.  this enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentPageProperty =
            DependencyProperty.Register("CurrentPage", typeof(Page), typeof(NavigationRootPage), new PropertyMetadata(null));

        public NavigationRootPage()
        {
            InitializeComponent();
            _designerPage = Resources["DesignerPage"] as Page;
            _dataSourcePage = Resources["DataSourcePage"] as Page;
            _ProtocolDesignerDock = Resources["ProtocolDock"] as UserControl;
            _UIDesignerDock = Resources["DesignerDock"] as UserControl;
            _ProtocolExpander = Resources["ProtocolExpander"] as UserControl;
            _UIExpander = Resources["UIExpander"] as UserControl;

            CurrentPage = _dataSourcePage;
            CurrentBorder = _ProtocolDesignerDock;
            CurrentExpender = _ProtocolExpander;
            
            DataContext = this;
            Loaded += delegate
            {
                PresetManager.Current.ColorPresetChanged += OnColorPresetChanged;

                //controlsSearchBox.Focus();
            };

            Unloaded += delegate
            {
                PresetManager.Current.ColorPresetChanged -= OnColorPresetChanged;
            };

            OnColorPresetChanged(null, null);




            List<ControllViewModel> controls = new List<ControllViewModel>
            {
                new ControllViewModel { ControlType = "Label", Title = "标签"},
                new ControllViewModel { ControlType = "Button" ,Title = "按钮"},
                new ControllViewModel { ControlType = "Grid", Title = "图表"},
                new ControllViewModel { ControlType = "RadioButton", Title = "单选框"},
                new ControllViewModel { ControlType = "Position", Title = "位姿"},
                };
            List<DataSourceViewModel> datasources = new List<DataSourceViewModel>
            {
                new DataSourceViewModel{ DataType = typeof(int), Source="串口1", Title="迭代次数"},
                new DataSourceViewModel{ DataType = typeof(float), Source="串口1", Title="经度"},
                new DataSourceViewModel{ DataType = typeof(float), Source="串口1", Title="纬度"},
                new DataSourceViewModel{ DataType = typeof(double), Source="Tcp源1", Title="旋转角"},
                new DataSourceViewModel{ DataType = typeof(byte), Source="Udp源1", Title="序列号"},
                new DataSourceViewModel{ DataType = typeof(string), Source="Udp源1", Title="用户ID"},
                new DataSourceViewModel{ DataType = typeof(bool), Source="Udp源1", Title="登录状态"},
                new DataSourceViewModel{ DataType = typeof(int), Source="串口1", Title="迭代次数"},
                new DataSourceViewModel{ DataType = typeof(float), Source="串口1", Title="经度"},
                new DataSourceViewModel{ DataType = typeof(float), Source="串口1", Title="纬度"},
                new DataSourceViewModel{ DataType = typeof(double), Source="Tcp源1", Title="旋转角"},
                new DataSourceViewModel{ DataType = typeof(byte), Source="Udp源1", Title="序列号"},
                new DataSourceViewModel{ DataType = typeof(string), Source="Udp源1", Title="用户ID"},
                new DataSourceViewModel{ DataType = typeof(bool), Source="Udp源1", Title="登录状态"},
            };
            List<DataSourceViewModel> dataTypes = new List<DataSourceViewModel>
            {
                new DataSourceViewModel{DataType = typeof(bool), Source="1位布尔值",Title=nameof(Boolean)},
                new DataSourceViewModel{DataType = typeof(sbyte), Source="8位有符号整型",Title=nameof(SByte)},
                new DataSourceViewModel{DataType = typeof(short), Source="16位有符号整型",Title=nameof(Int16)},
                new DataSourceViewModel{DataType = typeof(int), Source="32位有符号整型",Title=nameof(Int32)},
                new DataSourceViewModel{DataType = typeof(long), Source="64位有符号整型",Title=nameof(Int64)},
                new DataSourceViewModel{DataType = typeof(byte), Source="8位无符号整型",Title=nameof(Byte)},
                new DataSourceViewModel{DataType = typeof(ushort), Source="16位无符号整型",Title=nameof(UInt16)},
                new DataSourceViewModel{DataType = typeof(uint), Source="32位无符号整型",Title=nameof(UInt32)},
                new DataSourceViewModel{DataType = typeof(ulong), Source="64位无符号整型",Title=nameof(UInt64)},
                new DataSourceViewModel{DataType = typeof(float), Source="32位浮点数",Title=nameof(Single)},
                new DataSourceViewModel{DataType = typeof(double), Source="64位浮点数",Title=nameof(Double)},
                new DataSourceViewModel{DataType = typeof(string), Source="ASCII字符串",Title=nameof(String)}
            };
            //DataList.ItemsSource = new ObservableCollection<DataSourceViewModel>(dataTypes);
            //ControlsList.ItemsSource = new ObservableCollection<ControllViewModel>(controls);
            //DataList.ItemsSource = new ObservableCollection<DataSourceViewModel>(datasources);
        }

        private void SwitchDesigner(object sender, RoutedEventArgs e)
        {
            if (!(sender is ModernWpf.Controls.AppBarButton))
                return;
            else
                if (CurrentPage == _dataSourcePage)
            {
                CurrentPage = _designerPage;
                CurrentBorder = _UIDesignerDock;
                CurrentExpender = _UIExpander;
            }
            else
            {
                CurrentPage = _dataSourcePage;
                CurrentBorder = _ProtocolDesignerDock;
                CurrentExpender = _ProtocolExpander;
            }  
        }

        private void OnThemeButtonClick(object sender, RoutedEventArgs e)
        {
           
                if (ThemeManager.Current.ActualApplicationTheme == ApplicationTheme.Dark)
                {
                    ThemeManager.Current.ApplicationTheme = ApplicationTheme.Light;
                }
                else
                {
                    ThemeManager.Current.ApplicationTheme = ApplicationTheme.Dark;
                }
           
        }

        private void PresetMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource is MenuItem menuItem)
            {
                PresetManager.Current.ColorPreset = (string)menuItem.Header;
            }
        }

        private void OnColorPresetChanged(object sender, EventArgs e)
        {
                PresetsMenu.Items
                .OfType<RadioMenuItem>()
                .Single(mi => mi.Header.ToString() == PresetManager.Current.ColorPreset)
                .IsChecked = true;
        }


    }
}
